package com.fibrlink.adminassistant.index.activity;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.os.Bundle;
import android.view.MotionEvent;
import com.fibrlink.adminassistant.R;
import com.fibrlink.adminassistant.index.view.BrokenlineMarkerView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartGestureListener;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import java.util.ArrayList;
public class LineChartActivity extends Activity implements OnChartGestureListener,OnChartValueSelectedListener {
//线性折线图实例
private LineChart lineChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_chart);
lineChart = (LineChart) findViewById(R.id.chart1);
initLineChart();
}
private void initLineChart() {
lineChart.setOnChartGestureListener(this);
lineChart.setOnChartValueSelectedListener(this);
lineChart.setDrawGridBackground(false);
lineChart.animateX(2000);
// no description text
lineChart.getDescription().setEnabled(true);
lineChart.setViewPortOffsets(3,100,10,0);
// enable touch gestures
lineChart.setTouchEnabled(true);
// enable scaling and dragging
lineChart.setDragEnabled(true);
lineChart.setScaleEnabled(true);
// if disabled, scaling can be done on x- and y-axis separately
lineChart.setPinchZoom(false);
BrokenlineMarkerView brokenlineMarkerView = new BrokenlineMarkerView(getApplicationContext(),R.layout.custom_marker_view);
brokenlineMarkerView.setChartView(lineChart);
lineChart.setMarker(brokenlineMarkerView);
//坐标轴
XAxis axisX = lineChart.getXAxis();
axisX.setTextColor(Color.GRAY); //设置字体颜色
//axisX.setName("date"); //表格名称
axisX.setTextSize(10);//设置字体大小
axisX.setAxisMaximum(12); //最多几个X轴坐标,意思就是你的缩放让X轴上数据的个数7<=x<=mAxisXValues.length
axisX.setDrawGridLines(false);
axisX.setLabelCount(10);
axisX.setPosition(XAxis.XAxisPosition.BOTTOM);
setData(18, 30);
// lineChart.animateX(2500);
// lineChart.setDrawBorders(false);
//mChart.invalidate();
// get the legend (only possible after setting data)
Legend l = lineChart.getLegend();
// modify the legend ...
l.setForm(Legend.LegendForm.LINE);
}
private void setData(int count, float range) {
ArrayList<Entry> values = new ArrayList<Entry>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range) + 3;
values.add(new Entry(i, val));
}
LineDataSet set1;
if (lineChart.getData() != null &&
lineChart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet)lineChart.getData().getDataSetByIndex(0);
set1.setValues(values);
lineChart.getData().notifyDataChanged();
lineChart.notifyDataSetChanged();
} else {
// create a dataset and give it a type
set1 = new LineDataSet(values, "DataSet 1");
// set the line to be drawn like this "- - - - - -"
// set1.enableDashedLine(10f, 5f, 0f);
// set1.enableDashedHighlightLine(10f, 5f, 0f);
set1.setDrawCircles(false);
set1.setColor(Color.BLUE);
set1.setCircleColor(Color.BLUE);
set1.setLineWidth(1f);
set1.setCircleRadius(1f);
set1.setDrawCircleHole(false);
set1.setValueTextSize(9f);
//是否显示阴影
set1.setDrawFilled(false);
set1.setFormLineWidth(1f);
set1.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
set1.setFormSize(15.f);
set1.setFillColor(0);
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(set1); // add the datasets
// create a data object with the datasets
LineData data = new LineData(dataSets);
lineChart.setKeepPositionOnRotation(true);
// set data
lineChart.setData(data);
}
}
@Override
public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
}
@Override
public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
}
@Override
public void onChartLongPressed(MotionEvent me) {
}
@Override
public void onChartDoubleTapped(MotionEvent me) {
}
@Override
public void onChartSingleTapped(MotionEvent me) {
}
@Override
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
}
@Override
public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
}
@Override
public void onChartTranslate(MotionEvent me, float dX, float dY) {
}
@Override
public void onValueSelected(Entry e, Highlight h) {
}
@Override
public void onNothingSelected() {
}
}